﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
 

namespace YodaMaster.Models
{
    public class MyCart
    {
        public Carts db = new Carts();
        public string ScheduleCartId { get; set; }
        public const string CartSessionKey = "CartId";
        public static MyCart GetCart(HttpContextBase context)
        {
            var cart = new MyCart();
            cart.ScheduleCartId = cart.GetCartId(context);
            return cart;
        }

        // Helper method to simplify shopping cart calls
        public static MyCart GetCart(Controller controller)
        {
            return GetCart(controller.HttpContext);
        }

        public void AddToCart(Course course)
        {
            // Get the matching cart and album instances
            var cartItem = db.CartItems.ToList().SingleOrDefault(
                c => c.CartId == ScheduleCartId
                && c.id == course.number);

            if (cartItem == null)
            {
                // Create a new cart item if no cart item exists
                cartItem = new ScheduleCart
                {
                   
                    RecordId = db.CartItems.ToList().Count() + 1,
                    id = course.number,
                    name = course.name,
                    faculty = course.faculty,
                    points = course.points,
                    CartId = ScheduleCartId,    
                };
                db.CartItems.Add(cartItem);
            }

            // Save changes
             db.SaveChanges();
       
        }

        public void RemoveFromCart(int id)
        {
            // Get the matching cart and album instances
            var cartItem = db.CartItems.ToList().SingleOrDefault(
                c => c.CartId == ScheduleCartId
                && c.id == id);
            if (cartItem != null)
            {
                db.CartItems.Remove(cartItem);
            }

            // Save changes
            db.SaveChanges();
               
        }

        public void EmptyCart()
        {
            var cartItems = db.CartItems.ToList().Where(
                cart => cart.CartId == ScheduleCartId);

            foreach (var cartItem in cartItems)
            {
                db.CartItems.ToList().Remove(cartItem);
            }
            // Save changes
            db.SaveChanges();
        }
        public List<ScheduleCart> GetCartItems()
        {
            return db.CartItems.ToList().Where(
                cart => cart.CartId == ScheduleCartId).ToList();
        }

        public int GetCount()
        {
            var cartItems = db.CartItems.ToList().Where(
            cart => cart.CartId == ScheduleCartId);

            return cartItems.Count();
        }

        public double GetTotal()
        {
            double sum = 0;
            var cartItems = db.CartItems.ToList().Where(
            cart => cart.CartId == ScheduleCartId);
            foreach (var cartItem in cartItems)
            {
                sum += (double)cartItem.points;
            }
            return sum;
        }

        // We're using HttpContextBase to allow access to cookies.
        public string GetCartId(HttpContextBase context)
        {
            if (context.Session[CartSessionKey] == null)
            {
                if (!string.IsNullOrWhiteSpace(context.User.Identity.Name))
                {
                    context.Session[CartSessionKey] =
                        context.User.Identity.Name;
                }
                else
                {
                    // Generate a new random GUID using System.Guid class
                    Guid tempCartId = Guid.NewGuid();
                    // Send tempCartId back to client as a cookie
                    context.Session[CartSessionKey] = tempCartId.ToString();
                }
            }
            return context.Session[CartSessionKey].ToString();
        }

    

        
    }
}


